package com.dpwgc.alisalog.monitor.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dpwgc.alisalog.common.constant.RedisPrefix;
import com.dpwgc.alisalog.common.util.*;
import com.dpwgc.alisalog.monitor.base.PageBase;
import com.dpwgc.alisalog.monitor.model.response.LogResponse;
import com.dpwgc.alisalog.monitor.mapper.LogMapper;
import com.dpwgc.alisalog.monitor.model.request.LogRequest;
import com.dpwgc.alisalog.monitor.model.response.NodeResponse;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Set;

@Service
public class MonitorService {

    @Resource
    LogMapper logMapper;

    @Resource
    RedisUtil redisUtil;

    @Resource
    HttpUtil httpUtil;

    @Value("${router.url}")
    private String url;

    public PageBase<List<LogResponse>> getLogList(LogRequest logRequest) {

        //分页
        Page<LogResponse> page = new Page<>(logRequest.getPageIndex(), logRequest.getPageSize());

        //查询
        QueryWrapper<LogResponse> queryWrapper = new QueryWrapper<>();

        //检索条件
        if (logRequest.getIdc() != null && logRequest.getIdc().length() > 0){
            queryWrapper.eq("idc", logRequest.getIdc());
        }
        if (logRequest.getHost() != null && logRequest.getHost().length() > 0){
            queryWrapper.eq("host", logRequest.getHost());
        }
        if (logRequest.getEnv() != null && logRequest.getEnv().length() > 0){
            queryWrapper.eq("env", logRequest.getEnv());
        }
        if (logRequest.getTraceId() != null && logRequest.getTraceId().length() > 0){
            queryWrapper.eq("trace_id", logRequest.getTraceId());
        }
        if (logRequest.getModule() != null && logRequest.getModule().length() > 0){
            queryWrapper.eq("module", logRequest.getModule());
        }
        if (logRequest.getCategory() != null && logRequest.getCategory().length() > 0){
            queryWrapper.eq("category", logRequest.getCategory());
        }
        if (logRequest.getSubCategory() != null && logRequest.getSubCategory().length() > 0){
            queryWrapper.eq("sub_category", logRequest.getSubCategory());
        }
        if (logRequest.getFilter1() != null && logRequest.getFilter1().length() > 0){
            queryWrapper.eq("filter1", logRequest.getFilter1());
        }
        if (logRequest.getFilter2() != null && logRequest.getFilter2().length() > 0){
            queryWrapper.eq("filter2", logRequest.getFilter2());
        }
        if (logRequest.getLevel() != null && logRequest.getLevel() > 0){
            queryWrapper.eq("level", logRequest.getLevel());
        }
        if (logRequest.getTag() != null && logRequest.getTag().length() > 0){
            queryWrapper.eq("tag", logRequest.getTag());
        }
        if (logRequest.getKeyword() != null && logRequest.getKeyword().length() > 0){
            queryWrapper.like("title", logRequest.getKeyword())
                    .or().like("content", logRequest.getKeyword())
                    .or().like("remarks", logRequest.getKeyword())
                    .or().like("filter1", logRequest.getKeyword())
                    .or().like("filter2", logRequest.getKeyword());
        }

        queryWrapper.ge("log_time", logRequest.getLogTimeStart());
        queryWrapper.lt("log_time", logRequest.getLogTimeEnd());

        queryWrapper.eq("app_id", logRequest.getAppId());

        queryWrapper.orderByDesc("log_time");

        //装配返回
        Page<LogResponse> logMessagePage = logMapper.selectPage(page,queryWrapper);
        PageBase<List<LogResponse>> pageBase = new PageBase<>();
        pageBase.setList(logMessagePage.getRecords());
        pageBase.setTotal(logMessagePage.getTotal());
        return pageBase;
    }

    /**
     * 获取worker节点列表
     */
    public List<NodeResponse> getWorkerNodeList() {
        if (url == null || url.length() == 0) {
            return null;
        }
        String[] urls = url.split(",");
        for (String u : urls) {
            try {
                String res = httpUtil.doGet(String.format("%s%s",u,"/worker/node/list"));
                return JsonUtil.fromJson(res,List.class);
            } catch (Exception e) {
                LogUtil.error("NodeService list http doGet error",e.toString());
            }
        }
        return null;
    }

    /**
     * 获取router节点列表
     */
    public List<String> getRouterNodeList() {
        if (url == null || url.length() == 0) {
            return null;
        }
        String[] urls = url.split(",");
        return new ArrayList<>(Arrays.asList(urls));
    }

    /**
     * 获取数据中心列表
     */
    public Set<Object> getIdcList() {
        return redisUtil.sGet(RedisPrefix.IDC_SET);
    }

    /**
     * 获取应用id列表
     */
    public Set<Object> getAppIdList() {
        return redisUtil.sGet(RedisPrefix.APP_ID_SET);
    }

    /**
     * 获取环境列表
     */
    public Set<Object> getEnvList() {
        return redisUtil.sGet(RedisPrefix.ENV_SET);
    }

    /**
     * 根据数据中心名称获取旗下的主机列表
     */
    public Set<Object> getHostList(String idc) {
        return redisUtil.sGet(RedisKeyUtil.getHostListKey(idc));
    }

    /**
     * 根据应用id获取旗下的模块列表
     */
    public Set<Object> getModuleList(String appId) {
        return redisUtil.sGet(RedisKeyUtil.getModuleListKey(appId));
    }

    /**
     * 根据模块名称获取旗下的分类列表
     */
    public Set<Object> getCategoryList(String appId,String module) {
        return redisUtil.sGet(RedisKeyUtil.getCategoryListKey(appId,module));
    }

    /**
     * 根据分类名称获取旗下的子分类列表
     */
    public Set<Object> getSubCategoryList(String appId,String module,String category) {
        return redisUtil.sGet(RedisKeyUtil.getSubCategoryListKey(appId,module,category));
    }
}
